package oj_qmfx.oj_23;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    private static int[][] edge;
    private static boolean[] visited;
    private static int n;
    private static int m;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        edge = new int[n][n];
        visited = new boolean[n];

        for (int i = 0;i < m;i++){
            int x = sc.nextInt();
            int y = sc.nextInt();

            edge[x][y] = 1;
            edge[y][x] = 1;
        }

        BFS(0);
        sc.close();
    }
    public static void BFS(int x){
//        将起始点x标记已访问
        visited[x] = true;
        System.out.print(x + ",");
//        创建队列
        Queue<Integer> path = new LinkedList<>();
//        起始点添加进队列
        path.add(x);

//        队列如果不为空，就还需要做处理
        while (!path.isEmpty()){
            int cur = path.poll();  //头部取出一个顶点当做当前订单

//            遍历所有顶点
            for (int i = 0;i < n;i++){
//                如果是邻居
                if (edge[cur][i] == 1 && visited[i] == false){
                    path.add(i);    // 邻居添加至队列末尾
                    visited[i] = true;  //标记为已访问
                    System.out.print(i + ",");  //输入
                }
            }
        }
    }
}
